<template>
  <div class="app-container">
    <el-form ref="ruleForm" :model="ruleForm" status-icon :rules="rules" label-width="180px" class="ruleForm">
      <el-form-item label="旧密码" prop="oldPass">
        <el-input v-model="ruleForm.oldPass" type="password" placeholder="请输入旧密码"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPass">
        <el-input v-model="ruleForm.newPass" type="password" placeholder="请输入新密码" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="checkPass">
        <el-input v-model="ruleForm.checkPass" type="password" placeholder="确认新密码" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { updatePass } from "@/api/account";

  export default {
    data() {
      var checkOldPass = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入旧密码'));
        } else {
          if (value.length < 6) {
            callback(new Error('密码不能少于6位'));
          } else {
            callback();
          }
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
          if (value.length < 6) {
            callback(new Error('新密码不能少于6位'));
          } else {
            if (this.ruleForm.checkPass !== '') {
              this.$refs.ruleForm.validateField('checkPass');
            }
            callback();
          }
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入新密码'));
        } else if (value !== this.ruleForm.newPass) {
          callback(new Error('两次输入新密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          newPass: '',
          checkPass: '',
          oldPass: ''
        },
        rules: {
          newPass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          oldPass: [
            { validator: checkOldPass, trigger: 'blur' }
          ]
        }
      };
    },
    computed: {
    },
    created() {
    },
    methods: {
      submitForm(formName) {
        this.isSubmitLoading = true;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const ruleForm = {
              oldPass: this.$getRsaCode(this.ruleForm.oldPass),
              newPass: this.$getRsaCode(this.ruleForm.newPass)
            };
            updatePass(ruleForm).then(result => {
              this.$message({
                message: "密码修改成功，请使用新密码重新登录!",
                type: "success",
                duration: 5000,
                onClose: () => {
                  this.logout();
                }
              });

              this.isSubmitLoading = false;
            });
          } else {
            console.log('error submit!!');
            this.$message({
              message: this.$t("formIsNotValidMessage"),
              type: "warning"
            });
            this.isSubmitLoading = false;
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      async logout() {
        await this.$store.dispatch("account/resetToken");
        location.href = `/login?redirect=${this.$route.fullPath}`;
        // this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      }
    }
  }
</script>

<style lang="scss" scoped>
  .ruleForm {
    margin-top: 50px;
    max-width: 520px;
  }
</style>
